package cn.m3a1.task;

import cn.m3a1.dao.UserDao;
import cn.m3a1.model.*;
import com.iceking.dynamicweb.*;
import net.sf.json.JSONObject;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wyj
 * @date 2019/5/23
 * @time 下午2:55
 */
public class LoginTask  extends TaskProcessor {
    public void login () throws  Exception{
       toJsp("WEB-INF/login.html");
    }
    /**
     * 验证用户名和密码是否正确
     * @throws Exception
     */
    public void  sign() throws  Exception{
        System.out.println("执行了");
        JSONObject json=new JSONObject();
        String username = getParam("username");
        String password = getParam("password");
        System.out.println("md5"+GlobalUtils.md5Encode("admin"));
        if(GlobalUtils.isEmpty(username)||GlobalUtils.isEmpty(password)) {
            //判断内容是否为空
            //判断用户名密码是否存在
            json.put("success", false);
            json.put("stmt", "用户名或者密码不存在");
            toJson(json.toString());
            return;
        }
        //根据用户名和密码去进行数据库访问
        Pagination<User> pg=new Pagination<>();
        pg.appendWhere("where username=? and password=?",username,GlobalUtils.md5Encode(password));
        getDBConn().select(User.class, pg);
        User loginUser=pg.getFirstResult();
        if(loginUser==null) {
            //判断用户名密码是否正确
            json.put("success", false);
            json.put("stmt", "用户名不存在或者密码错误");
            toJson(json.toString());
            return;
        }
        getSession().setAttribute("loginUser",loginUser);
       // getSession().setAttribute("loginUser",loginUser);
        json.put("success",true);
        toJson(json.toString());

    }
    /**
     * 转发跳转到管理员管理页面
     * @throws Exception
     */
    public void home() throws  Exception{

        FrmkView frmkView;
        UserDao userDao=new UserDao();
        User loginUser = (User) getSession().getAttribute("loginUser");
        if (loginUser==null){
            return;
        }


        int type=loginUser.getType();

        if (type==99){
            //判断是否是超级管理员
            frmkView=new FrmkView("Administration.html");
            List list = userDao.findallUser(getDBConn());
            frmkView.addData("members",list);
            //判断是否是班主任
        }else if (loginUser.getType()==70){
            frmkView=new FrmkView("Headindex.html");
            //查询该班主任带的学生
            Pagination<Tclass> pg = new Pagination<>();
            pg.appendWhere("where managerid=?",loginUser.getId());
            getDBConn().select(Tclass.class, pg);
            List<Tclass> list = pg.getRows();
            frmkView.addData("tlist",list);
        }else if (loginUser.getType()==50){
            //判断是否是教师
            frmkView=new FrmkView("teacher1.html");
            //查询该老师的班级
            Pagination<Course> pg = new Pagination<>();
            pg.appendWhere("where teacherid=?",loginUser.getId());
            getDBConn().select(Course.class, pg);
            List<Course> list = pg.getRows();

            Pagination<Tclass> pgt = new Pagination<>();
            getDBConn().select(Tclass.class, pgt);
            Tclass tclass = pgt.getFirstResult();
            List<Tclass> tclasses = pgt.getRows();
            frmkView.addData("tclasses",tclasses);
            frmkView.addData("members",list);

        }else if (loginUser.getType()==1){
            //判断是否是学生
            frmkView=new FrmkView("stuleave.html");
            ArrayList<Course> courses= userDao.findcourse(getDBConn(),loginUser.getId());
           // System.out.println(list.get(0).getName());
            frmkView.addData("courses",courses);

        }else{
            //非法用户
            frmkView=new FrmkView("error.html");
        }

        frmkView.addData("loginUser",loginUser);

        toFreemarker(frmkView);

    }
    //退出
    public void logout() throws Exception{
        getSession().removeAttribute("loginUser");
        login ();

    }
}
